from PyQt5.QtWidgets import *
from PyQt5 import QtGui
import sys
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import pymysql
import base64
import io
import PIL.Image
import interface.teacher.tea_main0_Window as tea_main0_Window
import interface.teacher.add_course as add_course
import database.admin.setting as setting
class add_course_window(QMainWindow,add_course.Ui_Dialog):
    def __init__(self,id):
        QMainWindow.__init__(self)
        add_course.Ui_Dialog.__init__(self)
        self.id=id
        self.setupUi(self)
        self.setWindowTitle("添加课程")
        self.quit.clicked.connect(self.close)
        self.ok.clicked.connect(self.commit)
        self.init_pre_course()
    def commit(self):
        self.connect = pymysql.connect(**setting.config)
        self.cursor = self.connect.cursor()
        pre_course=self.pre_course_box.currentText()
        pre_id=None
        if(pre_course!='无'):
            #print(pre_course)
            sql_="select id from course where cname like %s "
            self.cursor.execute(sql_,pre_course)
            re=self.cursor.fetchone()
            pre_id=re[0]
        sql_="select * from teachers where id='%s'"%(self.id)
        self.cursor.execute(sql_)
        re=self.cursor.fetchone()
        tid=self.id
        tname=re[1]
        sql_="select count(*) from course "
        self.cursor.execute(sql_)
        re=self.cursor.fetchall()
        re=re[0][0]
        if(re<10):
            id='0000'+str(re+1)
        elif(re <100):
            id='000'+str(re+1)
        else:
            id='00'+str(re+1)
        cname=self.cname_l.text()
        credit=self.credit_box.currentText()
        book='《'+self.book_l.text()+'》'
        comment=self.comment.toPlainText()

        if(pre_id!=None):
            flag=True
            info=(id,cname,credit,tid,tname,book,pre_id,comment)
            for i in range(len(info)):
                if(info[i]==''):
                    print("Wrong")
                    flag=False
                    #msg_box = QMessageBox(QMessageBox.Warning, '警告', '文件出现异常')
                    QtWidgets.QMessageBox.critical(self, "错误", "提交请完善课程信息")
            print(info)
            if(flag==True):
                sql_="insert into course(id,cname,credit,tid,tname,book,pre_id,comments)values (%s,%s,%s,%s,%s,%s,%s,%s)"
                self.cursor.execute(sql_,info)
                print("try add")
                self.connect.commit()
                QMessageBox.about(self, "提示", "添加课程成功！")
        else:
            flag=True
            info = (id, cname, credit, tid, tname, book, comment)
            for i in range(len(info)):
                if(info[i]==''):
                    print("Wrong")
                    flag=False
                    #msg_box = QMessageBox(QMessageBox.Warning, '警告', '文件出现异常')
                    QtWidgets.QMessageBox.critical(self, "错误", "提交请完善课程信息")
            if(flag==True):
                sql_ = "insert into course(id,cname,credit,tid,tname,book,comments)values (%s,%s,%s,%s,%s,%s,%s)"
                print(info)
                self.cursor.execute(sql_,info)
                self.connect.commit()
                QMessageBox.about(self, "提示", "添加课程成功！")

        #print(info)

    def init_pre_course(self):
        self.pre_course_box.addItem('无')
        self.connect = pymysql.connect(**setting.config)
        self.cursor = self.connect.cursor()
        sql_="select cname from course "
        self.cursor.execute(sql_)
        re=self.cursor.fetchall()
        if(len(re)!=0):
            for i in range(len(re)):
                self.pre_course_box.addItem(re[i][0])


        self.cursor.close()
        self.connect.close()
        self.credit_box.addItem('0.5')
        self.credit_box.addItem('1')
        self.credit_box.addItem('1.5')
        self.credit_box.addItem('2')
        self.credit_box.addItem('2.5')
        self.credit_box.addItem('3')
        self.credit_box.addItem('3.5')
        self.credit_box.addItem('4')
        self.credit_box.addItem('4.5')
        self.credit_box.addItem('5')
        self.credit_box.addItem('5.5')
        self.credit_box.addItem('6')

if __name__=='__main__':
    app=QApplication(sys.argv)
    md=add_course_window('2018333333')
    md.show()
    sys.exit(app.exec_())


